//
//  MyOrderViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/3/31.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "MyOrderViewC.h"
#import "NotPayViewC.h"
#import "PayViewC.h"
#import "CancleViewC.h"
#import "Util.h"
#import "BusinessModel.h"
#import "UserModel.h"
#import "OrderEntity.h"
#import "SendCommtViewC.h"
#import <MBProgressHUD.h>
#import "AlipayViewC.h"

@interface NotPayCell : UITableViewCell
@property(nonatomic, weak) IBOutlet UILabel *aplaceLab;
@property(nonatomic, weak) IBOutlet UILabel *anameLab;
@property(nonatomic, weak) IBOutlet UILabel *adateLab;
@property(nonatomic, weak) IBOutlet UILabel *amoneyLab;
@property(nonatomic, weak) IBOutlet UIButton *cancleBtn;
@property(nonatomic, weak) IBOutlet UIButton *payBtn;
@end
@implementation  NotPayCell

@end

@interface PayCell : UITableViewCell
@property(nonatomic, weak) IBOutlet UILabel *bplaceLab;
@property(nonatomic, weak) IBOutlet UILabel *bnameLab;
@property(nonatomic, weak) IBOutlet UILabel *bdateLab;
@property(nonatomic, weak) IBOutlet UILabel *bmoneyLab;
@property(nonatomic, weak) IBOutlet UIButton *submitBtn;
@end
@implementation  PayCell

@end

@interface CancleCell : UITableViewCell
@property(nonatomic, weak) IBOutlet UILabel *cplaceLab;
@property(nonatomic, weak) IBOutlet UILabel *cnameLab;
@property(nonatomic, weak) IBOutlet UILabel *cdateLab;
@property(nonatomic, weak) IBOutlet UILabel *cmoneyLab;
@end
@implementation  CancleCell

@end

@interface MyOrderViewC (){
    
    NSMutableArray *dataArray;
    NSArray *clickArray;
}

@end

@implementation MyOrderViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title =@"我的订单";
    self.change =@"1";
    dataArray = [[NSMutableArray alloc] init];
    [self getData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)notpayBtn:(id)sender{
    [_payBtn setBackgroundColor:RGBCOLOR(201, 201, 201)];
    [_cancleBtn setBackgroundColor:RGBCOLOR(201, 201, 201)];
    [_notpayBtn setBackgroundColor:RGBCOLOR(16, 191, 229)];
    self.change =@"1";
    [self getData];
//    [_tableView reloadData];
}

- (IBAction)payBtn:(id)sender{
    [_payBtn setBackgroundColor:RGBCOLOR(16, 191, 229)];
    [_cancleBtn setBackgroundColor:RGBCOLOR(201, 201, 201)];
    [_notpayBtn setBackgroundColor:RGBCOLOR(201, 201, 201)];
    self.change =@"2";
    [self getData];
//    [_tableView reloadData];
}
- (IBAction)cancleBtn:(id)sender{
    [_payBtn setBackgroundColor:RGBCOLOR(201, 201, 201)];
    [_cancleBtn setBackgroundColor:RGBCOLOR(16, 191, 229)];
    [_notpayBtn setBackgroundColor:RGBCOLOR(201, 201, 201)];
    self.change =@"3";
    [self getData];
//    [_tableView reloadData];
}

- (void)getData{
    NSString *page =@"1";
    NSString *status = self.change;
    _idNumber = [NSString stringWithFormat:@"%@",[UserModel sharedInstance].idNum];
    NSString *idNum = _idNumber;
    [Util changeNSString:idNum];
    
    [[BusinessModel sharedInstance] getMyOrderWithSatus:status page:page idNum:idNum
            getMyOrder:^(id info) {
                NSArray *array = [info copy];
                dataArray = [NSMutableArray arrayWithArray:array];
                if (dataArray.count == 0) {
                    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                    HUD.labelText = @"没有数据";
                    [HUD hide:YES afterDelay:1.5f];
                }
                [self.tableView reloadData];
            }];


}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 126.f;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier;
    if ([self.change isEqual:@"1"]) {
        identifier = @"NotPayCellID";
        NotPayCell *cell =[tableView
                        dequeueReusableCellWithIdentifier:identifier
                                 forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        OrderEntity *orderEntity = dataArray[indexPath.row];
        cell.aplaceLab.text = orderEntity.sportHallName;
        cell.amoneyLab.text = [NSString stringWithFormat:@"%@.00",
                               orderEntity.totalPrice];
        cell.adateLab.text = [NSString stringWithFormat:@"日期：%@",
                              orderEntity.scheduledTime];
        cell.anameLab.text = [NSString stringWithFormat:@"项目：%@",
                              orderEntity.sportHallName];
        [cell.cancleBtn addTarget:self action:@selector(cancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.payBtn addTarget:self action:@selector(payBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.cancleBtn.tag = indexPath.row;
        cell.payBtn.tag = indexPath.row;
        return cell;
        
    }
    if ([self.change isEqual:@"2"]) {
        identifier =@"PayCellID";
        PayCell *cell =[tableView
                          dequeueReusableCellWithIdentifier:identifier
                        forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        OrderEntity *orderEntity = dataArray[indexPath.row];
        cell.bplaceLab.text = orderEntity.sportHallName;
        cell.bmoneyLab.text = [NSString stringWithFormat:@"%@.00",
                               orderEntity.totalPrice];
        cell.bdateLab.text = [NSString stringWithFormat:@"日期：%@",
                              orderEntity.scheduledTime];
        cell.bnameLab.text = [NSString stringWithFormat:@"项目：%@",
                              orderEntity.sportHallName];
//        UIButton *subbtn = [[UIButton alloc] init];
//        [subbtn addTarget:self action:@selector(submitBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//        cell.submitBtn = subbtn;
        return cell;
        
    }
    if ([self.change isEqual:@"3"]) {
        identifier =@"CancleCellID";
        CancleCell *cell =[tableView
                        dequeueReusableCellWithIdentifier:identifier
                             forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        OrderEntity *orderEntity = dataArray[indexPath.row];
        cell.cplaceLab.text = orderEntity.sportHallName;
        cell.cmoneyLab.text = [NSString stringWithFormat:@"%@.00",
                               orderEntity.totalPrice];
        cell.cdateLab.text = [NSString stringWithFormat:@"日期：%@",
                              orderEntity.scheduledTime];
        cell.cnameLab.text = [NSString stringWithFormat:@"项目：%@",
                              orderEntity.sportHallName];
            return cell;
    }
    
    else{
        
        UITableViewCell *cell;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

-(void)cancelBtnAction:(id) sender{
    UIButton *btn=(UIButton *)sender;
    OrderEntity *orEty = [dataArray objectAtIndex:btn.tag];
//    [dataArray removeObjectAtIndex:btn.tag];
    NSString *idNum = [NSString stringWithFormat:@"%@",orEty.ID];
    NSString *newsat =@"3";
    [[UserModel sharedInstance] changeOrderWithID:idNum status:newsat success:^(BOOL successed, unsigned int value) {
        if (successed) {
            [dataArray removeObjectAtIndex:btn.tag];
            [self.tableView reloadData];
        }
    }];
}

-(void)payBtnAction:(id) sender{
    UIButton *btn=(UIButton *)sender;
    OrderEntity *orEty = [dataArray objectAtIndex:btn.tag];
    NSDate*nowDate = [NSDate date];
    NSDateFormatter *nowFormatter=[[NSDateFormatter alloc]init];
    [nowFormatter setDateFormat:@"yyyy-mm-dd"];
    NSDateFormatter *otherFormatter=[[NSDateFormatter alloc]init];
    [otherFormatter setDateFormat:@"yyyy/mm/dd"];
    NSString *nowDatesub = [nowFormatter stringFromDate :nowDate];
    NSString *nowDatebody = [otherFormatter stringFromDate :nowDate];
    NSString *money = [NSString stringWithFormat:@"%@",orEty.totalPrice];
    NSString *num = [NSString stringWithFormat:@"%@",orEty.orderNum];
    
    [[UserModel sharedInstance] paymoneyWithMoney:money ordernum:num subject:nowDatesub body:nowDatebody paysuccess:^(id info) {
        NSData *reData = [info copy];
//    NSDictionary *data =@{@"money":money,@"OrderNum":num,
//                          @"Subject":nowDatesub,@"Body":nowDatebody};
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                   bundle:nil];
    AlipayViewC *payvc = [main instantiateViewControllerWithIdentifier:@"AlipayViewC"];
//    payvc.reciveDic = data;
        payvc.data = reData;
    [self.navigationController pushViewController:payvc animated:YES];
    }];
    
    

}

-(void)submitBtnAction:(id) sender{
    UIButton *btn=(UIButton *)sender;
    OrderEntity *orEty = [dataArray objectAtIndex:btn.tag];
    NSString *idNum = [NSString stringWithFormat:@"%@",orEty.ID];
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                   bundle:nil];
    SendCommtViewC *sendvc = [main instantiateViewControllerWithIdentifier:@"SendCommtViewC"];
    sendvc.sportId = idNum;
    [self.navigationController pushViewController:sendvc animated:YES];
}


@end
